<!DOCTYPE html>
<html lang="en">
<head>
    {include file="common/meta_new" /}
    <link rel="stylesheet" type="text/css" href="/static/css/windowDisplayAddressPage.css?v1">
    <script src="/assets/js2/layer.js"></script>
</head>
<body class="F6">
<div id="app" class="window-display-address-page">
    <!-- header -->
    <div class="header">
        <div class="header-height"></div>
        <div class="header-wrap">
            <div class="header-content">
                <div class="left">
                    <a href="{:url('index/user/index');}"><div class="go-back"><img src="/static/images/go-back.png" alt=""></div></a>
                    <div class="text">橱窗地址</div>
                </div>
                <div class="title"></div>
            </div>
        </div>
    </div>
    
    <div class="topBg">请上传橱窗二维码</div>

    <div class="winDispAddr" style="padding-top: 1.6rem">
        <div class="qrcodeBox">
            <img class="qrcode" src="{$user['qr_code']|default='/static/images/default_add_img.png'}" alt="">
        </div>
        
        <div class="selectImage">
<!--            <label for="imgUpload">
                <div class="pictureBox">
                    
                    {if $user['qr_code']==''}
                    <img src="/static/images/picture.png" alt="">
                    <div class="text">添加图片</div>
                    {else /}
                    <img src="/static/images/picture.png" alt="">
                    <div class="text"  >修改二维码</div>
                    {/if}
                </div>           
            </label>-->
            <input type="file" accept="image/*" onchange="sendFile(event)" id="imgUpload"/>
            <input type="hidden" id="filePath" value="{$user['qr_code']}">
        </div>
        <div>
        <label class="btn" id="edit" for="imgUpload" style="display: inline-block" >{if $user['qr_code']==''} 添加 {else /}修改{/if}</label>
        
            <input type="file" accept="image/*" onchange="sendFile(event)" id="imgUpload"/>
            <input type="hidden" id="filePath" value="<?php echo $user['qr_code']; ?>">
        <button class="btn" style="display: inline-block" onclick="sand()" >保存</button>
        </div>
    </div>

    <div class="tips">请正确上传自己的视频号橱窗二维码</div>
</div>
<script src="/static/js/common.js"></script>
<script>
    function upload_img(url,file_img, callback) {
        var formData = new FormData();
        formData.append('file', file_img);
        $.ajax({
            url: url, // 替换为你的上传脚本URL
            type: 'POST',
            data: formData,
            contentType: false,
            processData: false,
            success: function (res) {

                console.log(res);
//                    var res = {"code":0,"msg":"success","count":0,"data":{"msg":"上传成功","paths":["upload\/2024\/12\/03\/154121674eb6210fed0.jpg"],"names":["MSI_MAG.jpg"],"uris":["https:\/\/pic-cdn.wbball.com\/upload\/2024\/12\/03\/154121674eb6210fed0.jpg"],"size":[3237314],"exts":["jpg"],"reads":[],"is_imgs":[1],"code":0},"totalRow":[]}
                if (res.code == 1) {
                      $(".qrcode").attr('src',res.data.fullurl);
                      $("#filePath").val(res.data.url);
                      $("#edit").text("修改");
                }
            },
            error: function (xhr, status, error) {
                // 请求失败时的回调函数
                console.log('请求失败:', textStatus);
                console.log('详细错误信息:', errorThrown);
            }
        });
    }
    
    const sendFile = (files) => {
        const preview = document.querySelector('.qrcode');
        const file = document.querySelector('input[type=file]').files[0];
        var url = "{:url('api/common/upload')}";
        upload_img(url, file, function (res) {
             console.log(res);
         })
                       
    }
    function sand(){
        var filePath = $("#filePath").val();
        $.post("{:url('index/user/setqrcode')}",{"path":filePath},function(res){
                layer.msg(res.msg,{icon:1,time:3000,shade:0.4},function() {
                    var bak = "{$bak}";
                    if(bak!=''){
                        window.location =bak;
                    }
                    
                });
        });
    }
    
</script>
</body>
</html>